package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import entities.TCatalog;
import interfaces.ICatalogService;
import net.sf.json.JSONArray;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class CatalogAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ICatalogService catalogService;

	private TCatalog catBean;
	private List<TCatalog> catList;

	private String catNo;
	private String catName;
	private String catDad;
	private String catState;
	private Date catDate;
	private String catRemark;
	private Boolean catSummary;

	private String catTree;
	private Date catDateTop;
	private Date catDateEnd;

	// 新增或者修改入口
	// 根据主键获取对象,为空则创建带主键新对象
	public String edit() throws Exception {
		catBean = catalogService.fetch(catNo);
		if (StringUtil.isEmpty(catBean)) {
			catBean = new TCatalog();
		}
		return SUCCESS;
	}

	// 更新或保存数据
	// 数据库内已存在该主键则执行修改,不存在则执行新增
	public String save() throws Exception {
		catBean = new TCatalog();
		catBean.setCatNo(catNo);
		catBean.setCatName(catName);
		catBean.setCatDad(catDad);
		catBean.setCatState("1");
		catBean.setCatDate(new Date());
		catBean.setCatRemark(catRemark);
		catalogService.persist(catBean);
		return SUCCESS;
	}

	// 列表展示页面
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("catNo", StringUtil.split(catNo));
		paramMap.put("catName", catName);
		paramMap.put("catDad", catDad);
		paramMap.put("catDate", ParameterUtil.dateCombin(catDateTop, catDateEnd));
		
		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "catDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCatalog> resultSet = catalogService.search(querySet);
		catList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}
	
	public String tree() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("catDad", catDad);
		
		querySet.setPageInfo(new PageInfo(pageIndex, Integer.MAX_VALUE));
		querySet.setParamMap(paramMap);

		ResultSet<TCatalog> resultSet = catalogService.search(querySet);
		catList = resultSet.getResultList();
		catTree=JSONArray.fromObject(catList).toString();
		return SUCCESS;
	}
	
	public void route() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(catalogService.route(catNo, ""));
	}

	public void ajax() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("catDad", catDad);
		
		querySet.setPageInfo(new PageInfo(pageIndex, Integer.MAX_VALUE));
		querySet.setParamMap(paramMap);
		
		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "catNo" : sortField);
		sortInfo.setSortOrder(new Boolean(true));
		
		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCatalog> resultSet = catalogService.search(querySet);
		catList = resultSet.getResultList();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(JSONArray.fromObject(catList).toString());
	}


	// 导出数据
	// 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("catNo", StringUtil.split(catNo));
		paramMap.put("catName", catName);
		paramMap.put("catDad", catDad);
		paramMap.put("catDate", ParameterUtil.dateCombin(catDateTop, catDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "catDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCatalog> resultSet = catalogService.search(querySet);
		catList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("名称");
		fieldHead.add("父级编号");
		fieldHead.add("状态");
		fieldHead.add("日期");
		fieldHead.add("时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TCatalog catBean : catList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(catBean.getCatNo());
			fieldItem.add(catBean.getCatName());
			fieldItem.add(catBean.getCatDad());
			fieldItem.add(catBean.getCatState());
			fieldItem.add(DateUtil.format(catBean.getCatDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(catBean.getCatDate(), DateUtil.TIME));
			fieldItem.add(catBean.getCatRemark());
			fieldData.add(fieldItem);
		}
		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	public ICatalogService getCatalogService() {
		return catalogService;
	}

	public void setCatalogService(ICatalogService catalogService) {
		this.catalogService = catalogService;
	}

	public TCatalog getCatBean() {
		return catBean;
	}

	public void setCatBean(TCatalog catBean) {
		this.catBean = catBean;
	}

	public List<TCatalog> getCatList() {
		return catList;
	}

	public void setCatList(List<TCatalog> catList) {
		this.catList = catList;
	}

	public String getCatNo() {
		return catNo;
	}

	public void setCatNo(String catNo) {
		this.catNo = catNo;
	}

	public String getCatName() {
		return catName;
	}

	public void setCatName(String catName) {
		this.catName = catName;
	}

	public String getCatDad() {
		return catDad;
	}

	public void setCatDad(String catDad) {
		this.catDad = catDad;
	}

	public String getCatState() {
		return catState;
	}

	public void setCatState(String catState) {
		this.catState = catState;
	}

	public Date getCatDate() {
		return catDate;
	}

	public void setCatDate(Date catDate) {
		this.catDate = catDate;
	}

	public String getCatRemark() {
		return catRemark;
	}

	public void setCatRemark(String catRemark) {
		this.catRemark = catRemark;
	}

	public Boolean getCatSummary() {
		return catSummary;
	}

	public void setCatSummary(Boolean catSummary) {
		this.catSummary = catSummary;
	}

	public String getCatTree() {
		return catTree;
	}

	public void setCatTree(String catTree) {
		this.catTree = catTree;
	}

	public Date getCatDateTop() {
		return catDateTop;
	}

	public void setCatDateTop(Date catDateTop) {
		this.catDateTop = catDateTop;
	}

	public Date getCatDateEnd() {
		return catDateEnd;
	}

	public void setCatDateEnd(Date catDateEnd) {
		this.catDateEnd = catDateEnd;
	}
	

	//////////////////////////////////////

	
	
	
}
